//
//  StatusPictureView.swift
//  sinaBlog
//
//  Created by tianxiangyang on 15/12/18.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let pictureViewMargin:CGFloat = 5

let pictureViewWidth:CGFloat = ScreenWidth - statusMargin * 2

let pictureWidth:CGFloat = (pictureViewWidth - pictureViewMargin * 2) / 3

let pictureViewCellID = "pictureViewCellID"

class StatusPictureView: UICollectionView {
    
    
    var status:BlogStatus?{
    
        
        didSet{

   
        testLabel.text = "\(status!.pictureURLs?.count ?? 0)"
            
        let size = caculatePictureViewSize(status?.pictureURLs?.count ?? 0)
         
            
        self.snp_updateConstraints { (make) -> Void in
            make.size.equalTo(size)
            }
            
            reloadData()
        
        }
    }
 
   
    override init(frame: CGRect, collectionViewLayout layout:
        UICollectionViewLayout) {
            
        let layout = UICollectionViewFlowLayout()
            
        layout.minimumLineSpacing = pictureViewMargin
            
        layout.minimumInteritemSpacing = pictureViewMargin
            
        super.init(frame: frame, collectionViewLayout: layout)
            
        self.dataSource = self
            
        //注册cell
            
        self.registerClass(pictureViewCell.self, forCellWithReuseIdentifier: pictureViewCellID)
            
//        addSubview(testLabel)
//            
//        testLabel.snp_makeConstraints { (make) -> Void in
//            
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
//            
//            }
            
            
            setupUI()
            
            
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:设置UI
    private func setupUI(){
    
        
     backgroundColor = UIColor.whiteColor()
    
    
    }
    
    //MARK:计算图片视图的大小
    
    private func caculatePictureViewSize(pictureCount:Int) ->CGSize{
    
        
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout

        layout.itemSize = CGSize(width: pictureWidth, height: pictureWidth)
        
        if pictureCount == 0 {return CGSizeZero}

        else if pictureCount == 1 {
        
            layout.itemSize = CGSize(width: 180, height: 120)
            
            return CGSize(width: 180, height: 120)
        }
        
        
        else if pictureCount == 4 {
        
        return CGSize(width: pictureViewWidth - pictureWidth, height: pictureWidth * 2 + pictureViewMargin)
        
        }else{
        
         let row = CGFloat((pictureCount - 1) / 3 + 1)
   
        return CGSize(width: pictureViewWidth, height: pictureWidth * row + pictureViewMargin * (row - 1))
        
        }
    }
    
    
    lazy  var testLabel:UILabel = UILabel(title: "", text_Color: UIColor.redColor(), fontSize: 30, maxWidth: 0)
    

}

extension StatusPictureView:UICollectionViewDataSource{


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureURLs?.count ?? 0
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellID, forIndexPath: indexPath) as! pictureViewCell
        
        let url:NSURL = status!.pictureURLs![indexPath.item]
        
        cell.pictureImageView.sd_setImageWithURL(url)
        
        return cell
    }


}

class pictureViewCell:UICollectionViewCell{


    //MARK:必调的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    //MARK:设置UI
    func setupUI(){
    
        contentView.addSubview(pictureImageView)
        
        pictureImageView.snp_makeConstraints { (make) -> Void in
           make.edges.equalTo(contentView)
        }
    
    }
    
    //MARK:懒加载子控件
    
    lazy var pictureImageView:UIImageView = {
        
        let imageView = UIImageView()
        
        //设置图片原比例
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        //把超过显示size的部分减掉
        imageView.clipsToBounds = true
        
        return imageView
        }()
    
    
}


